package yuhao.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Demo1 {
    public static void main(String[] args) throws Exception {

        // 1.注册驱动
        //Class.forName("com.mysql.jdbc.Driver");


        // 2.获取数据库连接
        String url = "jdbc:mysql://127.0.0.1/db6?useSSL=false";
        String username = "root";
        String password = "1234";
        Connection conn = DriverManager.getConnection(url, username, password);


        String sql1 = "UPDATE account SET money=money+500 WHERE id=1";
        String sql2 = "UPDATE account SET money=money-500 WHERE id=2";
        Statement stmt = conn.createStatement();
        try {
            conn.setAutoCommit(false);

            int result1 = stmt.executeUpdate(sql1);
            System.out.println(result1);
            int result2 = stmt.executeUpdate(sql2);
            System.out.println(result2);
            conn.commit();

        } catch (Exception throwables) {
            conn.rollback();
            throwables.printStackTrace();
        } finally {
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }


        }




    }
}
